package com.tale.springbootatm.dao;

import com.tale.springbootatm.entity.TransactionRecord;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * author: 蔡奇峰
 * date: 2020/4/11 15:03
 * @Version V1.0
 **/
@Repository
public interface TransactionRecordRep extends JpaRepository<TransactionRecord, Long> {

    /**
     * 账单查询按交易时间排序
     * （这里把排序字段换成了主键id）
     *  因为主键使用自增策略
     * @param bankCard
     * @return
     */
//    @Query("from TransactionRecord t where t.bankCard = ?1 order by t.transactionDate DESC ")
    @Query("from TransactionRecord t where t.bankCard = ?1 order by t.id DESC ")
    List<TransactionRecord> OrderDate(int bankCard);


    /**
     * 分页查询
     * @param bankCard
     * @param pageable
     * @return
     */
    @Query("from TransactionRecord t where t.bankCard = ?1")
    Page<TransactionRecord> orderDatePage(int bankCard, Pageable pageable);

    /**
     * 通过卡号查询最近交易的一条记录
     *
     * @param bankCard
     * @return
     */
    TransactionRecord findByBankCardOrderById(int bankCard);

    /**
     * 通过卡号查询最近交易的一条记录
     *
     * @param banCard
     * @return
     */
    @Query("from TransactionRecord t where t.bankCard = ?1 order by t.id DESC ")
    List<TransactionRecord> findNearest(int banCard);

}
